package com.gxa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.vo.DrugInventoryVo;
import com.gxa.common.vo.DrugVo;
import com.gxa.entity.SysDrugInventory;
import com.gxa.mapper.DrugManagementMapper;
import com.gxa.mapper.SysDrugTotalMapper;
import com.gxa.service.SysDrugTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* 药品库存所有功能
* */
@Service
public class SysDrugTotalServiceImpl implements SysDrugTotalService {

    @Autowired
    SysDrugTotalMapper sysDrugTotalMapper;
    @Autowired
    DrugManagementMapper drugManagementMapper;

    @Override
    public R findDrugTotalList(Integer currentPage, Integer pageSize) {
        R r = new R();
        try {
            PageHelper.startPage(currentPage,pageSize);
            List<DrugInventoryVo> list = sysDrugTotalMapper.findDrugTotalList();
            PageInfo<DrugInventoryVo> pageInfo = new PageInfo(list);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;

    }
    static final Integer ADD =1;
    static final Integer SUB=0;
    @Override
    @Transactional(propagation= Propagation.REQUIRED,timeout=5,rollbackFor = {Exception.class})
    public void drugRecord(Integer type, Integer drugId, Integer num, String remark,Integer userId) {
        SysDrugInventory sysDrugInventory=new SysDrugInventory();
        if (ADD.equals(type)){
            drugManagementMapper.plusDrugNumById(num,drugId);
            DrugVo drug = drugManagementMapper.findDrugById(drugId);
            sysDrugInventory.setSerialNumber(drugId);
            sysDrugInventory.setNum(num);
            sysDrugInventory.setName(drug.getDrugName());
            sysDrugInventory.setTotal(drug.getNum());
            sysDrugInventory.setDisable(drug.getStatus());
            sysDrugInventory.setRemarks(remark);
            sysDrugInventory.setUserId(userId);
            sysDrugTotalMapper.drugRecord(sysDrugInventory);
        }else if(SUB.equals(type)){
            drugManagementMapper.subDrugNumById(num,drugId);
            DrugVo drug = drugManagementMapper.findDrugById(drugId);
            sysDrugInventory.setSerialNumber(drugId);
            sysDrugInventory.setNum(num);
            sysDrugInventory.setName(drug.getDrugName());
            sysDrugInventory.setTotal(drug.getNum());
            sysDrugInventory.setDisable(drug.getStatus());
            sysDrugInventory.setRemarks(remark);
            sysDrugInventory.setUserId(userId);
            sysDrugTotalMapper.drugRecord(sysDrugInventory);
        }


    }


    /**
     * 根据日志id修改日志信息
     * @param remark 备注
     * @param id 日志id
     * @return R
     */
    @Override
    public R updateByRemark(String remark, Integer id) {
        R r=new R();
        if (remark!=null&&id!=null){
            try{
                sysDrugTotalMapper.updateByRemark(remark,id);
                r.setCode(200);
                r.setMsg("success");
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }

        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }


    /**
     * 根据日志id回显信息
     * @param id 日志id
     * @return R
     */
    @Override
    public R selectById(Integer id) {
        R r=new R();
        if (id!=null){
            try{
                SysDrugInventory sysDrugInventory = sysDrugTotalMapper.selectById(id);
                r.setCode(200);
                r.setMsg("success");
                r.setData(sysDrugInventory);
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 根据条件模糊查询日志
     * @param condition 模糊条件
     * @param pageSize 每页大小
     * @param currentPage 当前页
     * @return R
     */
    @Override
    public R selectDrugTotalByCondition(String condition,Integer pageSize,Integer currentPage) {
        R r=new R();
        if (condition!=null){
            try{
                PageHelper.startPage(currentPage,pageSize);
                List<DrugInventoryVo> inventoryVoList = sysDrugTotalMapper.selectDrugTotalByCondition(condition);
                PageInfo<DrugInventoryVo> pageInfo = new PageInfo(inventoryVoList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(pageInfo);
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }
}

